package com.ytkj.digitalConstruction.service.project;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ytkj.digitalConstruction.dao.dept.DeptProjectMapper;
import com.ytkj.digitalConstruction.dao.dept.DeptUserMapper;
import com.ytkj.digitalConstruction.dao.project.ProjectMapper;
import com.ytkj.digitalConstruction.entity.dept.Dept;
import com.ytkj.digitalConstruction.entity.dept.DeptProject;
import com.ytkj.digitalConstruction.entity.dept.DeptUser;
import com.ytkj.digitalConstruction.entity.project.Project;
import com.ytkj.digitalConstruction.httpbean.project.ResProject;
import com.ytkj.digitalConstruction.service.dept.DeptService;

/**
 * 项目业务处理类
 * 
 * @author Pete
 * @Date 2017年12月7日
 */
@Service
public class ProjectService {

	@Autowired
	private DeptService deptService;

	@Autowired
	private ProjectMapper projectMapper;

	@Autowired
	private DeptProjectMapper deptProjectMapper;

	@Autowired
	private DeptUserMapper deptUserMapper;

	/**
	 * 添加项目
	 * 
	 * @param project
	 * @return
	 */
	@Transactional
	public boolean addProject(Project project, HttpServletRequest request) {
		// 在项目组中添加相应的数据
		projectMapper.insertSelective(project);
		DeptProject deptProject = new DeptProject();
		deptProject.setDeptId(project.getDeptId());
		deptProject.setProjectId(project.getProjectId());
		// 在关联表中添加数据
		deptProjectMapper.insert(deptProject);
		return true;
	}

	/**
	 * 查询用户的所有项目
	 * 
	 * @param userId
	 */
	public List<ResProject> getProjects(Integer userId) {
		List<ResProject> lists = new ArrayList<>();
		List<DeptUser> list = deptUserMapper.selectByUserId(userId);
		// 循环查寻所有的项目
		for (DeptUser deptUser : list) {
			// 判断人员所在的部门
			Dept deptb = deptService.selectByDeptId(deptUser.getDeptId());
			if (deptb.getDeptType() == 2) {
				lists.addAll(projectMapper.queryByDeptIds(deptUser.getDeptId()));
			}
			if (deptb.getDeptType() == 1) {
				lists.addAll(projectMapper.queryByDeptIdss(deptUser.getDeptId()));
			}

		}
		for (ResProject resProject : lists) {
			resProject.setArrdeptId(getIdListByString(resProject.getDeptIds()));
			resProject.setConstructDeptIds(getIdListByString(projectMapper.selectids(resProject.getConstructDeptId())));
			resProject
					.setSupervisorDeptIds(getIdListByString(projectMapper.selectids(resProject.getSupervisorDeptId())));
			resProject.setDetectionDeptIds(getIdListByString(projectMapper.selectids(resProject.getDetectionDeptId())));
		}
		return lists;
	}

	/**
	 * 根据ids字符串获取ids列表
	 * 
	 * @param ids
	 * @return
	 */
	private List<Integer> getIdListByString(String ids) {
		List<Integer> returnList = new ArrayList<Integer>();
		if (ids == null || ids.equals("")) {
			return returnList;
		}
		String[] idsArr = ids.split(",");
		for (String id : idsArr) {
			if (id == null || id.equals("null")) {
				continue;
			}
			returnList.add(Integer.parseInt(id));
		}
		return returnList;
	}

	/**
	 * 根据项目Id与角色Id列表查询用户Id列表
	 * 
	 * @param projectId
	 * @param roleIds
	 * @return
	 */
	public List<Integer> getUserIdListByprojectIdAndRoleIds(Integer projectId, List<Integer> roleIds) {
		// 根据项目Id查询项目组
		Project project = projectMapper.selectByPrimaryKey(projectId);
		// 根据项目部Id与角色Id列表查询用户Id列表
		return deptService.getUserIdListBydeptIdAndRoleId(project.getDeptId(), roleIds);
	}

	/**
	 * 修改项目
	 * 
	 * @param project
	 */
	@Transactional
	public boolean updateProject(Project project) {
		// 判断deptId是否为空
		DeptProject deptProject = new DeptProject();
		deptProject.setDeptId(project.getDeptId());
		deptProject.setProjectId(project.getProjectId());

		Integer deptId = project.getDeptId();
		if (deptId != null && deptId > 1) {
			// 修好改相关联的表中数据 upshot
			deptProjectMapper.updateByPrimaryKey(deptProject);
		}
		// 判断修改项目是否成功
		Integer result = projectMapper.updateByPrimaryKey(project);
		if (result == null || result == 0) {
			return false;
		}
		return true;
	}

	/**
	 * 查询部门
	 * 
	 * @param deptId
	 * @return
	 */
	public Dept selectByDeptId(Integer deptId) {
		Dept dept = deptService.selectByDeptId(deptId);
		return dept;

	}

}
